package control

import (
	"dockerui/models"
	ut "dockerui/utils"
	"github.com/gin-gonic/gin"
	"strconv"
)

func DockerImagesEndPoint(c *gin.Context) {
	var (
		skip int
		limit int
		imageSearch string
		err error
		imageRes []models.ImageFormatResp
		respLen int
		respData models.ImagesResp
	)
	if skip, err = strconv.Atoi(
		c.DefaultQuery("skip", "0")); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	}
	if limit, err = strconv.Atoi(
		c.DefaultQuery("limit", "10")); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	}
	imageSearch = c.DefaultQuery("search", "")
	//fmt.Println("skip", skip, "limit", limit)
	if imageRes, respLen, err = dockerImages(skip, limit, imageSearch); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	} else {
		respData = models.ImagesResp{
			Images: imageRes,
			Len: respLen,
		}
		c.JSON(200, gin.H{
			"Code": ut.SuccessCode,
			"Msg": ut.SuccessMsg,
			"Data": respData,
		})
	}
}

func DockerContainersEndPoint(c *gin.Context) {
	var (
		skip int
		limit int
		search string
		err error
		containerRes []models.ContainerFormatResp
		respLen int
		respData models.ContainerResp
	)
	if skip, err = strconv.Atoi(
		c.DefaultQuery("skip", "0")); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	}
	if limit, err = strconv.Atoi(
		c.DefaultQuery("limit", "10")); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	}
	search = c.DefaultQuery("search", "")
	//fmt.Println("skip", skip, "limit", limit)
	if containerRes, respLen, err = dockerContainer(skip, limit, search); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	} else {
		respData = models.ContainerResp{
			Containers: containerRes,
			Len: respLen,
		}
		c.JSON(200, gin.H{
			"Code": ut.SuccessCode,
			"Msg": ut.SuccessMsg,
			"Data": respData,
		})
	}
}

func DockerNetworksEndpoint(c *gin.Context) {
	var (
		skip int
		limit int
		search string
		err error
		networksRes []models.NetworksFormatResp
		respLen int
		respData models.NetworksResp
	)
	if skip, err = strconv.Atoi(
		c.DefaultQuery("skip", "0")); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	}
	if limit, err = strconv.Atoi(
		c.DefaultQuery("limit", "10")); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	}
	search = c.DefaultQuery("search", "")
	if networksRes, respLen, err = dockerNetworks(skip, limit, search); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	} else {
		respData = models.NetworksResp{
			Networks: networksRes,
			Len: respLen,
		}
		c.JSON(200, gin.H{
			"Code": ut.SuccessCode,
			"Msg": ut.SuccessMsg,
			"Data": respData,
		})
	}
}

func ContainerInspectEndpoint(c *gin.Context) {
	var (
		containerId string
		err error
		containerInspectRes map[string]interface{}
		respLen int
		respData models.ContainerInspectResp
	)
	containerId = c.DefaultQuery("containerId", "")
	if containerInspectRes, respLen, err = dockerContainerInspect(containerId); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	} else {
		respData = models.ContainerInspectResp{
			ContainerInfo: containerInspectRes,
			Len: respLen,
		}
		c.JSON(200, gin.H{
			"Code": ut.SuccessCode,
			"Msg": ut.SuccessMsg,
			"Data": respData,
		})
	}
}


func NetworkInspectEndpoint(c *gin.Context) {
	var (
		networkId string
		networkScope string
		networkVerbose string
		verbose bool
		err error
		networkInspectRes map[string]interface{}
		respLen int
		respData models.NetworkInspectResp
	)
	networkId = c.DefaultQuery("networkId", "")
	// 暂时不太清楚此参数含义，选择默认
	networkScope = c.DefaultQuery("scope", "local")
	// 暂时不太清楚此参数含义，选择为空
	networkVerbose = c.DefaultQuery("verbose", "")
	if networkVerbose == "" {
		verbose = false
	} else {
		verbose = true
	}
	if networkInspectRes, respLen, err = dockerNetworkInspect(networkId, networkScope, verbose); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	} else {
		respData = models.NetworkInspectResp{
			NetworkInfo: networkInspectRes,
			Len: respLen,
		}
		c.JSON(200, gin.H{
			"Code": ut.SuccessCode,
			"Msg": ut.SuccessMsg,
			"Data": respData,
		})
	}
}

func CliInfoEndpoint(c *gin.Context) {
	var (

		err error
		CliInfoRes models.DockerCliInfo
		respLen int
		respData models.CliInfoResp
	)
	if CliInfoRes, respLen, err = clientInfo(); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	} else {
		respData = models.CliInfoResp{
			CliInfo: CliInfoRes,
			Len: respLen,
		}
		c.JSON(200, gin.H{
			"Code": ut.SuccessCode,
			"Msg": ut.SuccessMsg,
			"Data": respData,
		})
	}
}


func SearchImageEndpoint(c *gin.Context) {
	var (
		searchName string
		err error
		searchImageRes []models.SearchImageFormatResp
		respLen int
		respData models.SearchImagesResp
	)

	 searchName= c.DefaultQuery("imageName", "")
	if searchImageRes, respLen, err = searchImages(searchName); err != nil {
		c.JSON(200, gin.H{
			"Code": ut.ErrorCode,
			"Msg": err.Error(),
			"Data": nil,
		})
	} else {
		respData = models.SearchImagesResp{
			Images:searchImageRes,
			Len: respLen,
		}
		c.JSON(200, gin.H{
			"Code": ut.SuccessCode,
			"Msg": ut.SuccessMsg,
			"Data": respData,
		})
	}
}

//func PullImageEndPoint(c *gin.Context) {
//	var (
//		pullImage string
//		err error
//	)
//
//	pullImage = c.DefaultQuery("imageName", "")
//	if err = dockerImagePull(pullImage); err != nil {
//		c.JSON(200, gin.H{
//			"Code": ut.ErrorCode,
//			"Msg": err.Error(),
//			"Data": nil,
//		})
//	} else {
//		c.JSON(200, gin.H{
//			"Code": ut.SuccessMsg,
//			"Msg": ut.SuccessMsg,
//			"Data": nil,
//		})
//	}
//}
